package com.lill.system.web;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.lill.common.component.util.PageUtils;
import com.lill.common.component.util.Query;
import com.lill.common.component.util.R;
import com.lill.support.annotation.CurrentUser;
import com.lill.system.constant.SystemConstant;
import com.lill.system.model.SysDomainCompanyModel;
import com.lill.system.model.SysDomainEntity;
import com.lill.system.model.SysUserEntity;
import com.lill.system.service.ISysDomainService;

/**
 * 域对象Controller
 *
 * @author lill
 */
@RestController
@RequestMapping("/sys/domain")
public class SysDomainController {
    @Autowired
    private ISysDomainService domainService;

    /**
     * 查看列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("sys:domain:list")
    public R list(@RequestParam Map<String, Object> params,@CurrentUser SysUserEntity curUser) {
    	 if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		 params.put("isAll", "true");
    	 }
        //查询列表数据
        Query query = new Query(params);
        List<SysDomainEntity> domainList = domainService.queryList(query);
        int total = domainService.queryTotal(query);
        PageUtils pageUtil = new PageUtils(domainList, total, query.getLimit(), query.getPage());

        return R.ok().put("page", pageUtil);
    }

    /**
     * 查看信息
     */
    @RequestMapping("/info/{id}")
//    @RequiresPermissions("sys:domain:info")
    public R info(@PathVariable("id") String id) {
        SysDomainEntity domain = domainService.queryObject(id);

        return R.ok().put("domain", domain);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("sys:domain:save")
    public R save(@RequestBody SysDomainEntity domain,@CurrentUser SysUserEntity user) {
        int result=domainService.save(domain,user);

        if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("sys:domain:update")
    public R update(@RequestBody SysDomainEntity domain,@CurrentUser SysUserEntity user) {
        int result=domainService.update(domain,user);

        if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("sys:domain:delete")
    public R delete(@RequestBody String[] ids,@CurrentUser SysUserEntity user) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	params.put("ids", ids);
    	params.put("updateUser", user.getId());
    	params.put("updateTime", new Date());
    	
    	int result=domainService.deleteLogicByMap(params);
    	if(result>0){
    		return R.ok();
        }else{
        	return R.failure();
        }
    }

    /**
     * 查看所有列表
     */
    @RequestMapping("/queryAll")
    public R queryAll(@CurrentUser SysUserEntity curUser,@RequestParam Map<String, Object> params) {
        List<SysDomainEntity> list=null;
        if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
        	params.put("isAll", "true");
            list = domainService.queryList(params);
        } else {
            params.put("userId", curUser.getId());
            list = domainService.queryList(params);
        }
        return R.ok().put("list", list);
    }
    
    /**
     * 查看用户所有列表
     */
    @RequestMapping("/queryUserAll")
    public R queryUserAll(@CurrentUser SysUserEntity curUser) {
    	Map<String, Object> params=new HashMap<String, Object>();
    	List<SysDomainEntity> list=null;
    	if (SystemConstant.SUPER_ADMIN.equals(curUser.getId())) {
    		params.put("type", "A");
    		params.put("status", 1);
    		list = domainService.queryList(params);
    		return R.ok().put("list", list);
    	} else {
    		params.put("userId", curUser.getId());
    		List<SysDomainCompanyModel> listDomain = domainService.queryUserAll(params);
    		return R.ok().put("list", listDomain);
    	}
    }
    
    @RequestMapping("/queryAllByNoConditions")
    public R queryAllByNoConditions(@CurrentUser SysUserEntity curUser,@RequestParam Map<String, Object> params) {
    	List<SysDomainEntity> list=domainService.queryList(params);
    	return R.ok().put("list", list);
    }
}
